package com.suemi.rpc.entity;

public class RpcMessage {

  private RpcHeader header;

  private Object body;

  public RpcHeader getHeader() {
    return header;
  }

  public void setHeader(RpcHeader header) {
    this.header = header;
  }

  public Object getBody() {
    return body;
  }

  public void setBody(Object body) {
    this.body = body;
  }

  @Override
  public String toString() {
    return "RpcMessage{" +
            "header=" + header +
            ", body=" + body +
            '}';
  }

  public static class RpcMessageBuilder {

    private RpcHeader header = new RpcHeader();

    private Object body;

    public RpcMessageBuilder crc(long code) {
      header.setCrcCode(code);
      return this;
    }

    public RpcMessageBuilder length(int len) {
      header.setLength(len);
      return this;
    }

    public RpcMessageBuilder sender(long senderId) {
      header.setSender(senderId);
      return this;
    }

    public RpcMessageBuilder type(MessageType type) {
      header.setType(type);
      return this;
    }

    public RpcMessageBuilder version(byte version) {
      header.setVersion(version);
      return this;
    }

    public RpcMessageBuilder attachment(String key, String value) {
      header.getAttachments().put(key, value);
      return this;
    }

    public RpcMessageBuilder body(Object body) {
      this.body = body;
      return this;
    }

    public RpcMessageBuilder reset() {
      header = new RpcHeader();
      body = null;
      return this;
    }

    public RpcMessage build() {
      RpcMessage message = new RpcMessage();
      message.setBody(body);
      message.setHeader(header);
      return message;
    }

  }
}
